package me.yiyou.meihua.ui.page.itemsnumber

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
import me.yiyou.meihua.element.CustomButton

@Composable
fun ItemNumber() {

    val viewModel = ItemNumberViewModel()
    val inputContent = viewModel.inputContent.value
    val showDescribe = viewModel.showDescribe.value

    if (showDescribe) {
        Column(
            Modifier
                .fillMaxWidth()
                .padding(top = 80.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            val focusManager = LocalFocusManager.current
            Text(text = "请输入可数之物数量")
            OutlinedTextField(
                value = inputContent,
                onValueChange = viewModel::inputChangeValue,
                label = { Text(text = "输入数字") },
                placeholder = { Text(text = "请输入数字") },
                keyboardOptions = KeyboardOptions(imeAction = ImeAction.Go),
                keyboardActions = KeyboardActions(
                    onSearch = {
                        viewModel.start()
                        focusManager.clearFocus() // 收起键盘
                    }
                ),
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(10.dp)
            )
            CustomButton(
                text = "确定",
                onClick = { viewModel.start() },
                modifier = Modifier
                    .width(100.dp)
                    .padding(10.dp)
            )
        }
    }
}